package com.qf.java2106.service.impl;


import com.qf.java2106.common.ResultVO;
import com.qf.java2106.constant.MessageConstant;
import com.qf.java2106.dao.IFoodDao;
import com.qf.java2106.dao.IFoodTypeDao;
import com.qf.java2106.pojos.Food;
import com.qf.java2106.pojos.PageBean;
import com.qf.java2106.service.IFoodService;
import jdk.nashorn.internal.ir.CallNode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.qf.java2106.constant.MessageConstant.*;

/**
 * @author q
 * @version V1.0
 * @date 2021/10/26
 **/
public class FoodServiceImpl implements IFoodService {

    ApplicationContext applicationContext =  new ClassPathXmlApplicationContext("applicationContext.xml");
    IFoodDao foodDao = (IFoodDao) applicationContext.getBean("foodDao");
    IFoodTypeDao foodTypeDao = (IFoodTypeDao) applicationContext.getBean("foodTypeDao");

    @Override
    public ResultVO findFoodLikeKey(String keyword) {
        try {
            if (StringUtils.isEmpty(keyword)){
                keyword="%%";
            } else {
                keyword="%"+ keyword.trim() + "%";
            }
            List<Food> foodLikeKey = foodDao.findFoodLikeKey(keyword);
            List<Map<String, Object>> foodList = new ArrayList<>();
            for (Food food : foodLikeKey) {
                food.setFoodType(foodTypeDao.findTypeById(food.getTypeId()));
                //Map<String, Object> foodMap = JSONObject.parseObject(JSON.toJSONString(food));
                //foodList.add(foodMap);
            }
            //System.out.println(foodList);
            return ResultVO.ok(FOOD_QUERY_SUCCESS,foodLikeKey);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOOD_QUERY_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO findFoodById(Integer id) {
        try {
            Food foodById = foodDao.findFoodById(id);
            foodById.setFoodType(foodTypeDao.findTypeById(foodById.getTypeId()));
            return ResultVO.ok(FOOD_QUERY_SUCCESS,foodById);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOOD_QUERY_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO saveFood(Food food) {
        try {
            return ResultVO.ok(FOOD_SAVE_SUCCESS,foodDao.saveFood(food));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOOD_SAVE_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO findFoodByName(String foodName) {
        try {
            if (null==foodName){
                return ResultVO.error(FOODNAME_IS_NOT_NULL);
            }
            if (null != foodDao.findFoodByName(foodName)){
                return ResultVO.error(FOODNAME_IS_EXISTS);
            } else {
                return ResultVO.ok(FOODNAME_IS_NOT_EXISTS);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(e.getMessage());
        }
    }

    @Override
    public ResultVO updateFood(Food food) {
        try {
            return ResultVO.ok(FOOD_UPDATE_SUCCESS,foodDao.updateFood(food));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOOD_UPDATE_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO deleteFood(Integer id) {
        try {
            return ResultVO.ok(FOOD_DELETE_SUCCESS,foodDao.deleteFood(id));
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOOD_DELETE_FAIL,e.getMessage());
        }
    }

    @Override
    public ResultVO findPageFood(Integer pageNum, Integer pageSize, Integer typeId, String foodName) {
        try {
            if (pageNum <= 0){
                pageNum = 1;
            }
            if (pageSize <= 0){
                pageSize = 6;
            }

            //没点select
            if (null != foodName){
                //foodName = "null";
                foodName = "%" + foodName.trim() + "%";
            } else {
                foodName = "%%";
            }
            /*if ("null" != foodName){
                if (!StringUtils.isEmpty(foodName)){
                    foodName = "%" + foodName.trim() + "%";
                } else {
                    foodName = "%%";
                }
            }*/

            PageBean<Food> pageFood = (PageBean<Food>) applicationContext.getBean("pageBean");

            Long totalCounts = foodDao.findTotalCounts(typeId, foodName);//todo
            pageFood.setTotalCounts(totalCounts);
            pageFood.setTotalPages();

            if (0 == totalCounts){
                pageNum = 1;
            } else if (pageNum > pageFood.getTotalPages()){
                pageNum = pageFood.getTotalPages();
            }

            pageFood.setCurrentPage(pageNum);
            pageFood.setPageSize(pageSize);

            int start = (pageNum - 1) * pageSize;

            List<Food> foodList = foodDao.findPageFood(start, pageSize, typeId, foodName);
            pageFood.setList(foodList);

            return ResultVO.ok(FOOD_PAGE_SUCCESS,pageFood);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultVO.error(FOOD_PAGE_FAIL);
        }
    }
}
